package com.buer.storage.api;


import com.buer.storage.business.domain.Prod;
import com.buer.storage.business.dto.ProdDto;
import com.buer.storage.business.service.ProdService;
import com.buer.storage.common.annotation.Auth;
import com.buer.storage.common.eumn.ResultEnum;
import com.buer.storage.common.utils.BaseResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

//装备信息管理
@RestController
@RequestMapping("/prod")
public class ProdController {
    @Resource
    private ProdService prodService;

    //增加装备
    @PostMapping("/add.do")
    @Auth
    public BaseResult add(@RequestBody ProdDto prodDto) {
        if (prodService.add(prodDto) > 0) {
            //添加装备成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //添加装备失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //删除装备
    @GetMapping("/delete.do")
    @Auth
    public BaseResult delete(int prodId) {
        if (prodService.delete(prodId) > 0) {
            //删除装备成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //删除装备失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //修改装备
    @PostMapping("/update.do")
    @Auth
    public BaseResult update(@RequestBody Prod prod){
        if (prodService.update(prod)>0){
            //修改装备成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //修改装备失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //查询装备
    @GetMapping("/select.do")
    @Auth
    public BaseResult select(){

        List<ProdDto> select = prodService.select();
        return BaseResult.build(ResultEnum.SUCCESS,select);
    }

    //通过名字或者编号查询
    @GetMapping("/selectByName")
    @Auth
    public BaseResult selectByName( String prodName,String prodCode){

        List<Prod> prod = prodService.selectByName(prodName,prodCode);
        return BaseResult.build(ResultEnum.SUCCESS,prod);
    }

    //通过装备分类查询
    @GetMapping("/selectByCategory.do")
    @Auth
    public BaseResult selectByCategory(int categoryId){
        List<ProdDto> prod = prodService.selectByCategory(categoryId);
        return BaseResult.build(ResultEnum.SUCCESS,prod);
    }

    @PostMapping("/cardupload.do")
    @Auth
    public BaseResult upload(MultipartFile file){
        return prodService.upload(file);
    }
}
